<?php

// +----------------------------------------------------------------------
// | 核心函数
// +----------------------------------------------------------------------


use app\Route;
use app\core\Config;


Route::fun('读/:path/:key/:value', function ($path, $key, $value = null) {
    $config = Config::get('app');
    $savePath = $config['data_route'] . '/' . $path;
    if (file_exists($savePath)) {
        $data = unserialize(file_get_contents($savePath));
        if (array_key_exists($key, $data)) {
            return $data[$key];
        }
    }
    return $value;
});

Route::fun('写/:path/:key/:value', function ($path, $key, $value = null) {
    $config = Config::get('app');
    $savePath = $config['data_route'] . '/' . $path;
    if (file_exists($savePath)) {
        $data = unserialize(file_get_contents($savePath));
        $data[$key] = $value;
    } else {
        $data[$key] = $value;
    }
    if (!is_dir(dirname($savePath))) {
        @mkdir(dirname($savePath), 0777, true);
    }
    file_put_contents($savePath, serialize($data));
});

Route::fun('访问/:url', function ($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $output = curl_exec($ch);
    curl_close($ch);
    echo "运行结果；".$output;
    return $output;
});


Route::fun('随机数/:interval', function ($interval) {
    $array = explode('-', $interval);
    return mt_rand($array[0], $array[1]);
});


Route::fun('回调/:instruction', function ($instruction) {
    $className = Route::getClassName($instruction);
    if (method_exists($this, $className)) {
        return $this->$className();
    }
});

Route::fun('括号/:num', function ($num) {
    return $this->bracket[$num];
});

Route::fun('参数/:num', function ($num) {
    return $this->matches[$num];
});

Route::fun('时间/:date', function ($date) {
    $date = str_replace(['dd'], ['d'], $date);
    return date($date);

});

Route::var('QQ', function () {
    return $this->message['uin'];
});

Route::var('群号', function () {
    return $this->message['gin'];
});

Route::var('Robot', function () {
    return $this->message['bot'];
});


/**
 * 测试用，正式使用请删除下列代码
 */
Route::var('主人', function () {
    return $this->message['uin'];
});
